package com.bluemoon.model.test.dbImort.core;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import com.bluemoon.model.test.dbImort.file.DbResourceFile;
import com.bluemoon.model.test.dbImort.report.IResultReportHandler;
import com.bluemoon.model.test.dbImort.report.ResultReportFactory;
import com.bluemoon.model.test.dbImort.transform.DefaultResultTransformHandler;
import com.bluemoon.model.test.dbImort.transform.IResultTransformHandler;
import com.bluemoon.model.test.dbImort.visitor.po.ExtractResultSet;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Jarod.Kong
 * @date 2021/2/4 15:52
 */
@Slf4j
public abstract class FileImportTemplate<SourceModel, DbModel> {
    private final DbResourceFile dbResourceFile;
    private ExtractResultSet<SourceModel> resultSet;
    private IResultReportHandler<SourceModel> resultReportHandler;
    private IResultTransformHandler<SourceModel, DbModel> resultTransformHandler;

    public FileImportTemplate(DbResourceFile dbResourceFile) {
        this.dbResourceFile = dbResourceFile;
        BmAssetUtils.isTrue(dbResourceFile != null, "文件类不可为空");
        DatabaseType dbType = dbResourceFile.getDbType();
        BmAssetUtils.isTrue(dbType != null, "请指定数据库类型");
        if (resultReportHandler == null) {
            this.resultReportHandler = ResultReportFactory.resultReportHandler(dbType);
        }
        if (resultTransformHandler == null) {
            this.resultTransformHandler = new DefaultResultTransformHandler<>();
        }
    }

    public ResultBean<Boolean> fileImport() {
        // 解析
        try {
            fileParser();
        } catch (Exception e) {
            log.error("解析文件失败！", e);
            return ResultBean.error("解析文件失败");
        }
        // 校验
        boolean checkResultOk = resultReportHandler.validate(resultSet);
        if (!checkResultOk) {
            return ResultBean.error("文件内容校验不通过");
        }
        // 结果集转换
        ExtractResultSet<DbModel> dbResultSet = resultTransformHandler.transform(resultSet);
        // 入库
        insertDb(dbResultSet);
        return ResultBean.ok(true);
    }

    protected void insertDb(ExtractResultSet<DbModel> dbResultSet) {
        log.info("入库" + dbResultSet);

    }

    protected void fileParser() throws Exception {
        System.out.println(dbResourceFile);
        this.resultSet = ExtractResultSet.empty();
    }

    public ExtractResultSet<SourceModel> getResultSet() {
        return resultSet;
    }

    public DbResourceFile getDbResourceFile() {
        return dbResourceFile;
    }

    public FileImportTemplate<SourceModel, DbModel> setResultReportHandler(IResultReportHandler<SourceModel> resultReportHandler) {
        this.resultReportHandler = resultReportHandler;
        return this;
    }

    public FileImportTemplate<SourceModel, DbModel> setResultTransformHandler(IResultTransformHandler<SourceModel, DbModel> resultTransformHandler) {
        this.resultTransformHandler = resultTransformHandler;
        return this;
    }
}
